﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ART.BLL;
using ART.Common;

namespace ART.Admini.Users
{
    public partial class RolesDetail : System.Web.UI.Page
    {
        private ART.BLL.B_Roles post = new ART.BLL.B_Roles();
        private ART.BLL.B_permissions desktop = new ART.BLL.B_permissions();
        private ART.Model.M_Roles model = new ART.Model.M_Roles();

        private int postid = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            postid = LYRequest.GetInt("id", 0);
            if (!IsPostBack)
            {
                InitData();
            }
        }
        private void InitData()
        {
            JBind.BindList(repPermissions, desktop.GetList(0, "Id,PermissName", "parentid=0", "Sort asc").Tables[0]);
            if (postid > 0)
            {
                model = post.GetModel(postid);
                this.txtPostName.Text = model.RoleName;
                this.txtRemarks.Text = model.Remarks;
                this.txtSort.Text = model.Sort.ToString();
                this.setCheckbox(model.PermissionsIds);
            }
        }
        protected void btnSave_Click(object sender, EventArgs p1)
        {
            model.PermissionsIds = getCheckBox();
            model.RoleName = this.txtPostName.Text.Trim();
            model.Remarks = this.txtRemarks.Text.Trim();
            model.Sort = int.Parse(this.txtSort.Text.Trim());

            if (postid == 0)
            {

                if (this.post.Add(model) > 0)
                {
                    B_operationLog.CreateOperationLog(B_operationLog.operationLogType.Add, "角色", model.RoleName);
                    MessageBox.JSLoad(sender, "alert('保存成功!');reloadDlg();");
                }
            }
            else
            {
                model.Id = postid;
                if (this.post.Update(model))
                {
                    B_operationLog.CreateOperationLog(B_operationLog.operationLogType.Edit, "角色", model.RoleName);
                    MessageBox.JSLoad(sender, "alert('保存成功!');reloadDlg();");
                }
            }
        }
        private string getCheckBox()
        {
            StringBuilder builder = new StringBuilder();
            foreach (RepeaterItem item in this.repPermissions.Items)
            {
                CheckBox box = (CheckBox)item.FindControl("cheFirst");
                if (box.Checked)
                {
                    builder.Append(box.ToolTip).Append(",");
                }
                Repeater repeater = (Repeater)item.FindControl("repItemPermissions");
                foreach (RepeaterItem item2 in repeater.Items)
                {
                    CheckBox box2 = (CheckBox)item2.FindControl("cheSecond");
                    if (box2.Checked)
                    {
                        builder.Append(box2.ToolTip).Append(",");
                    }
                    CheckBoxList list = (CheckBoxList)item2.FindControl("chelPermissionsIds");
                    foreach (ListItem item3 in list.Items)
                    {
                        if (item3.Selected)
                        {
                            builder.Append(item3.Value).Append(",");
                        }
                    }
                }
            }
            builder.Append(0);
            return builder.ToString();
        }
        private void setCheckbox(string p0)
        {
            string[] array = p0.Split(new char[] { ',' });
            foreach (RepeaterItem ri in repPermissions.Items)
            {
                CheckBox box = (CheckBox)ri.FindControl("cheFirst");
                if (Array.IndexOf<string>(array, box.ToolTip) > -1)
                {
                    box.Checked = true;
                }
                Repeater repeater = (Repeater)ri.FindControl("repItemPermissions");
                foreach (RepeaterItem item2 in repeater.Items)
                {
                    CheckBox box2 = (CheckBox)item2.FindControl("cheSecond");
                    if (Array.IndexOf<string>(array, box2.ToolTip) > -1)
                    {
                        box2.Checked = true;
                    }
                    CheckBoxList list = (CheckBoxList)item2.FindControl("chelPermissionsIds");
                    foreach (ListItem item3 in list.Items)
                    {
                        if (Array.IndexOf<string>(array, item3.Value) > -1)
                        {
                            item3.Selected = true;
                        }
                    }
                }
            }
        }
        protected void cheSecond_CheckedChanged(object sender, EventArgs p1)
        {
            foreach (RepeaterItem ri in repPermissions.Items)
            {
                Repeater repeater = (Repeater)ri.FindControl("repItemPermissions");
                foreach (RepeaterItem item2 in repeater.Items)
                {
                    CheckBox box = (CheckBox)item2.FindControl("cheSecond");
                    CheckBox box2 = (CheckBox)sender;
                    CheckBoxList list = (CheckBoxList)item2.FindControl("chelPermissionsIds");
                    if (box.ToolTip == box2.ToolTip)
                    {
                        if (box2.Checked)
                        {
                            foreach (ListItem item3 in list.Items)
                            {
                                item3.Selected = true;
                            }
                        }
                        else
                        {
                            foreach (ListItem item3 in list.Items)
                            {
                                item3.Selected = false;
                            }
                        }
                    }
                }
            }

        }
        protected void repItemPermissions_ItemDataBound(object sender, RepeaterItemEventArgs p1)
        {
            if ((p1.Item.ItemType == ListItemType.Item) || (p1.Item.ItemType == ListItemType.AlternatingItem))
            {
                HiddenField field = (HiddenField)p1.Item.FindControl("hidItemPermissionsId");
                CheckBoxList list = (CheckBoxList)p1.Item.FindControl("chelPermissionsIds");
                list.DataSource = desktop.GetList(0, string.Format("parentid={0}", field.Value), "Sort,Id");
                list.DataBind();
            }
        }

        protected void repPermissions_ItemDataBound(object sender, RepeaterItemEventArgs p1)
        {
            if ((p1.Item.ItemType == ListItemType.Item) || (p1.Item.ItemType == ListItemType.AlternatingItem))
            {
                HiddenField field = (HiddenField)p1.Item.FindControl("hidPermissionsId");
                Repeater repeater = (Repeater)p1.Item.FindControl("repItemPermissions");
                repeater.DataSource = desktop.GetList(0, string.Format("parentid={0}", field.Value), "Sort,Id");
                repeater.DataBind();
            }
        }
    }
}
